//
//  LoginManager.m
//  CBT
//
//  Created by Mr.Zhang on 2018/11/28.
//  Copyright © 2018 Mr.Zhang. All rights reserved.
//

#import "LoginManager.h"

@implementation LoginManager

+(void)phoneaAndEmailLoginWithID:(NSString *)ID isPhone:(BOOL)phone pwd:(NSString *)pwd area:(NSString *)area block:(loginManager_result_block)block
{
    if (phone) {
        NSMutableDictionary * param = [[NSMutableDictionary alloc]init];
        [param setObject:ID forKey:@"phone"];
        [param setObject:pwd forKey:@"passWord"];
        [param setObject:area forKey:@"area"];
        [NetWork requestWithURL:Network_Phone_Login params:param httpMethod:@"POST" finishedBlock:^(NSURLSessionDataTask *task, id  _Nullable result) {
            block(result,[result[@"code"]intValue] == 100 ? kHttpRequestResultNormal : kHttpRequestResultError,result[@"message"]);
        } failureBlock:^(NSURLSessionDataTask *task, NSError *error) {
            block(nil,kHttpRequestResultError,kLocalizedString(@"网络异常，请稍后重试",nil));
        }];
    }
    else {
        NSMutableDictionary * param = [[NSMutableDictionary alloc]init];
        [param setObject:ID forKey:@"email"];
        [param setObject:pwd forKey:@"passWord"];
        [NetWork requestWithURL:Network_Email_Login params:param httpMethod:@"POST" finishedBlock:^(NSURLSessionDataTask *task, id  _Nullable result) {
            block(result,[result[@"code"]intValue] == 100 ? kHttpRequestResultNormal : kHttpRequestResultError,result[@"message"]);
        } failureBlock:^(NSURLSessionDataTask *task, NSError *error) {
            block(nil,kHttpRequestResultError,kLocalizedString(@"网络异常，请稍后重试",nil));
        }];
    }
}


+(void)phoneaAndEmailRegisteredWithID:(NSString *)ID area:(NSString *)area isPhone:(BOOL)phone code:(NSString *)code pwd:(NSString *)pwd avatar:(NSString *)avatar isSetPassword:(BOOL)setPassword block:(loginManager_result_block)block
{
    if (setPassword) {
        NSMutableDictionary * param = [[NSMutableDictionary alloc]init];
        [param setObject:ID forKey:@"phone"];
        [param setObject:pwd forKey:@"passWord"];
        [param setObject:code forKey:@"code"];
        [param setObject:area forKey:@"area"];
        [param setObject:avatar forKey:@"avatar"];
        [NetWork requestWithURL:Network_Set_Password params:param httpMethod:@"POST" finishedBlock:^(NSURLSessionDataTask *task, id  _Nullable result) {
            block(result,[result[@"code"]intValue] == 100 ? kHttpRequestResultNormal : kHttpRequestResultError,result[@"message"]);
        } failureBlock:^(NSURLSessionDataTask *task, NSError *error) {
            block(nil,kHttpRequestResultError,kLocalizedString(@"网络异常，请稍后重试",nil));
        }];
    }
    else {
        if (phone) {
            
            NSMutableDictionary * param = [[NSMutableDictionary alloc]init];
            [param setObject:ID     forKey:@"phone"];
            [param setObject:pwd    forKey:@"passWord"];
            [param setObject:code   forKey:@"code"];
            [param setObject:area   forKey:@"area"];
            [param setObject:avatar forKey:@"avatar"];
            [NetWork requestWithURL:Network_AddUserByPhone params:param httpMethod:@"POST" finishedBlock:^(NSURLSessionDataTask *task, id  _Nullable result) {
                block(result,[result[@"code"]intValue] == 100 ? kHttpRequestResultNormal : kHttpRequestResultError,result[@"message"]);
            } failureBlock:^(NSURLSessionDataTask *task, NSError *error) {
                block(nil,kHttpRequestResultError,kLocalizedString(@"网络异常，请稍后重试",nil));
            }];
            
        }else{
            
            NSMutableDictionary * param = [[NSMutableDictionary alloc]init];
            [param setObject:ID forKey:@"email"];
            [param setObject:pwd forKey:@"passWord"];
            [param setObject:code forKey:@"code"];
            [param setObject:avatar forKey:@"avatar"];
            [NetWork requestWithURL:Network_AddUserByEmail params:param httpMethod:@"POST" finishedBlock:^(NSURLSessionDataTask *task, id  _Nullable result) {
                block(result,[result[@"code"]intValue] == 100 ? kHttpRequestResultNormal : kHttpRequestResultError,result[@"message"]);
            } failureBlock:^(NSURLSessionDataTask *task, NSError *error) {
                block(nil,kHttpRequestResultError,kLocalizedString(@"网络异常，请稍后重试",nil));
            }];
        }
    }
}

+(void)sendPhoneAndEmailCodeWithID:(NSString *)ID area:(NSString *)area isPhone:(BOOL)phone type:(NSString *)type block:(loginManager_result_block)block
{
    if (phone) {
        NSMutableDictionary * param = [[NSMutableDictionary alloc]init];
        [param setObject:ID forKey:@"phone"];
        [param setObject:type forKey:@"type"];
        [param setObject:area forKey:@"code"];
        [NetWork requestWithURL:Network_SendPhoneMsg params:param httpMethod:@"POST" finishedBlock:^(NSURLSessionDataTask *task, id  _Nullable result) {
            block(result,[result[@"code"]intValue] == 100 ? kHttpRequestResultNormal : kHttpRequestResultError,result[@"message"]);
        } failureBlock:^(NSURLSessionDataTask *task, NSError *error) {
             block(nil,kHttpRequestResultError,kLocalizedString(@"网络异常，请稍后重试",nil));
        }];
    }else{
        NSMutableDictionary * param = [[NSMutableDictionary alloc]init];
        [param setObject:ID forKey:@"email"];
        [param setObject:type forKey:@"type"];
        [NetWork requestWithURL:Network_SendEmailMsg params:param httpMethod:@"POST" finishedBlock:^(NSURLSessionDataTask *task, id  _Nullable result) {
            block(result,[result[@"code"]intValue] == 100 ? kHttpRequestResultNormal : kHttpRequestResultError,result[@"message"]);
        } failureBlock:^(NSURLSessionDataTask *task, NSError *error) {
            block(nil,kHttpRequestResultError,kLocalizedString(@"网络异常，请稍后重试",nil));
        }];
    }
}
@end
